﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class EditRecipeItemDialog : ChildWindow
    {
        public ServiceReferenceOpenRPOS.RecipeSummary rs;
        public ServiceReferenceOpenRPOS.RecipeDetail rd;

        public EditRecipeItemDialog()
        {
            InitializeComponent();
        }

        public void init()
        {
            // Load ingredients
            cmbIngredient.Items.Clear();
            foreach (ServiceReferenceOpenRPOS.Ingredient ingr in Globals.db.IngredientRecords)
                cmbIngredient.Items.Add(ingr.col_Name);
            if (cmbIngredient.Items.Count() > 0)
                cmbIngredient.SelectedIndex = 0;
            // Load units
            cmbUnit.Items.Clear();
            foreach (ServiceReferenceOpenRPOS.Unit unit in Globals.db.UnitRecords)
                cmbUnit.Items.Add(unit.col_Name);
            if (cmbUnit.Items.Count() > 0)
                cmbUnit.SelectedIndex = 0;
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Save the recipe detail
            rd.col_RecipeSummaryID = rs.col_ID;
            rd.bFilterRecipeSummaryID = true;
            if (cmbIngredient.SelectedIndex >= 0)
            {
                rd.col_IngredientID = Globals.db.IngredientRecords[cmbIngredient.SelectedIndex].col_ID;
                rd.bFilterIngredientID = true;
            }
            if (cmbUnit.SelectedIndex >= 0)
            {
                rd.col_UnitID = Globals.db.UnitRecords[cmbUnit.SelectedIndex].col_ID;
                rd.bFilterUnitID = true;
            }
            try { rd.col_Quantity = Convert.ToDouble(tbQuantity.Text); rd.bFilterQuantity = true; }
            catch (Exception) { }
            tbQuantity.Text = rd.col_Quantity.ToString("N");
            await Globals.db.UpdateRecipeDetailObject(rd);
            Globals.menuItems.initRecipes(true);
            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void cmbMenuCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }
         
    }
}

